package com.test.manager.impl;

import java.util.Arrays;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import org.apache.commons.lang.StringUtils;
import org.apache.ibatis.annotations.Param;
import org.slf4j.ILoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.test.common.Page;
import com.test.common.Query;
import com.test.common.SystemConstant.StatusType;
import com.test.entity.SysUser;
import com.test.manager.SysUserManager;
import com.test.mapper.SysMenuMapper;
import com.test.mapper.SysRoleMapper;
import com.test.mapper.SysUserMapper;
import com.test.mapper.SysUserRoleMapper;

/**
 * 系统用户
 *
 * @author luotuo
 */
@Component("sysUserManager")
public class SysUserManagerImpl implements SysUserManager {

	@Autowired
	private SysUserMapper sysUserMapper;

	@Autowired
	private SysMenuMapper sysMenuMapper;

	@Autowired
	private SysRoleMapper sysRoleMapper;

	@Autowired
	private SysUserRoleMapper sysUserRoleMapper;

	@Override
	public List<SysUser> listUser(Page<SysUser> page, Query search) {
		return sysUserMapper.listForPage(page, search);
	}

	@Override
	public SysUser getByUserName(@Param(value = "userName") String userName) {
		System.out.println("账号：" + userName);
		SysUser user = null;
		try {
			user = sysUserMapper.getByUserName(userName);
		} catch (Exception e) {
			System.out.println(e.getMessage());
		}
		return user;
	}

	@Override
	public int saveUser(SysUser user) {
		int count = sysUserMapper.save(user);
		Query query = new Query();
		query.put("userId", user.getUser_id());
		query.put("roleIdList", user.getRoleIdList());
		sysUserRoleMapper.save(query);
		return count;
	}

	@Override
	public SysUser getById(Long userId) {
		SysUser user = sysUserMapper.getObjectById(userId);
		user.setRoleIdList(sysUserRoleMapper.listUserRoleId(userId));
		return user;
	}

	@Override
	public int updateUser(SysUser user) {
		int count = sysUserMapper.update(user);
		Long userId = user.getUser_id();
		sysUserRoleMapper.remove(userId);
		Query query = new Query();
		query.put("userId", userId);
		query.put("roleIdList", user.getRoleIdList());
		sysUserRoleMapper.save(query);
		return count;
	}

	@Override
	public int batchRemove(Long[] id) {
		int count = sysUserMapper.batchRemove(id);
		sysUserRoleMapper.batchRemoveByUserId(id);
		return count;
	}

	@Override
	public Set<String> listUserPerms(Long userId) {
		List<String> perms = sysMenuMapper.listUserPerms(userId);
		Set<String> permsSet = new HashSet<>();
		for (String perm : perms) {
			if (StringUtils.isNotBlank(perm)) {
				permsSet.addAll(Arrays.asList(perm.trim().split(",")));
			}
		}
		return permsSet;
	}

	@Override
	public Set<String> listUserRoles(Long userId) {
		List<String> roles = sysRoleMapper.listUserRoles(userId);
		Set<String> rolesSet = new HashSet<>();
		for (String role : roles) {
			if (StringUtils.isNotBlank(role)) {
				rolesSet.addAll(Arrays.asList(role.trim().split(",")));
			}
		}
		return rolesSet;
	}

	@Override
	public int updatePswdByUser(Query query) {
		return sysUserMapper.updatePswdByUser(query);
	}

	@Override
	public int updateUserEnable(Long[] id) {
		Query query = new Query();
		query.put("status", StatusType.ENABLE.getValue());
		query.put("id", id);
		int count = sysUserMapper.updateUserStatus(query);
		return count;
	}

	@Override
	public int updateUserDisable(Long[] id) {
		Query query = new Query();
		query.put("status", StatusType.DISABLE.getValue());
		query.put("id", id);
		int count = sysUserMapper.updateUserStatus(query);
		return count;
	}

	@Override
	public int updatePswd(SysUser user) {
		return sysUserMapper.updatePswd(user);
	}

	@Override
	public SysUser getUserById(Long userId) {// 不包含角色信息
		return sysUserMapper.getObjectById(userId);
	}

}
